package com.steam.org.controller;

import com.steam.basic.util.AjaxResult;
import com.steam.basic.util.PageList;
import com.steam.org.domain.Department;
import com.steam.org.query.DepartmentQuery;
import com.steam.org.service.IDepartmentService;
import com.steam.sys.annotation.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "部门接口类", description = "部门接口类")
@RestController
@RequestMapping("/department")
class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "查询单个")
    //根据id查询
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id")Long id){
        return departmentService.loadById(id);
    }

    @ApiOperation(value = "查询全部")
    //查询全部
    @GetMapping
    public List<Department> findAll(){
        return departmentService.loadAll();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id")Long id){
        try {
            departmentService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败!");
        }
    }

    @ApiOperation(value = "添加和修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            System.out.println(department);
            if (department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败!");
        }
    }

    @ApiOperation(value = "分页查询")
    @PostMapping
    @PreAuthorize(name = "部门列表",sn = "department:list") //value后面用来做权限校验
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return departmentService.queryPage(query);
    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败!");
        }
    }

    @ApiOperation(value = "获取部门树-无限极接口")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }
}
